package com.example.demo.utils;


import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendBatchSmsRequest;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendBatchSmsResponse;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsResponse;
import com.google.gson.Gson;
import darabonba.core.client.ClientOverrideConfiguration;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

public class smsUtil {


    public static Map<String, String> sendSms(String phone) {
        String accessKeyId = "";// 填写自己创建accessKeyId
        String accessKeySecret = ""; // 填写自己创建accessKeySecret
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId(accessKeyId)
                .accessKeySecret(accessKeySecret)
                .build());
        AsyncClient client = AsyncClient.builder()
                .region("cn-shenzhen") // Region ID
                .credentialsProvider(provider)
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setEndpointOverride("dysmsapi.aliyuncs.com")
                )
                .build();

        SendSmsRequest sendSmsRequest =  SendSmsRequest.builder()
                .signName("残月的博客") // 这里需要填写自己的签名名称
                .templateCode("")// 这里需要填写自己的短信模板code
                .phoneNumbers(phone)
                .build();
        CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest);
        try {
            SendSmsResponse resp = response.get();
            System.out.println(new Gson().toJson(resp));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            client.close();
        }
        Map<String, String> codeMap = new HashMap<>();
        codeMap.put(sendSmsRequest.getPhoneNumbers(), sendSmsRequest.getTemplateCode());
        return codeMap;
    }

    public static Map<String, String> batchSendSms(List<String> phone) {
        String accessKeyId = "";// 填写自己创建accessKeyId
        String accessKeySecret = ""; // 填写自己创建accessKeySecret

        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId(accessKeyId)
                .accessKeySecret(accessKeySecret)
                .build());

        AsyncClient client = AsyncClient.builder()
                .region("cn-shenzhen") // Region ID
                .credentialsProvider(provider)
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setEndpointOverride("dysmsapi.aliyuncs.com")
                )
                .build();

        SendBatchSmsRequest sendBatchSmsRequest =  SendBatchSmsRequest.builder()
                .signNameJson("残月的博客") // 这里需要填写自己的签名名称
                .templateCode("")// 这里需要填写自己的短信模板code
                .phoneNumberJson(phone.toString())
                .build();
        CompletableFuture<SendBatchSmsResponse> response = client.sendBatchSms(sendBatchSmsRequest);
        try {
            SendBatchSmsResponse resp = response.get();
            System.out.println(new Gson().toJson(resp));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            client.close();
        }
        Map<String, String> codeMap = new HashMap<>();
        codeMap.put(sendBatchSmsRequest.getPhoneNumberJson(), sendBatchSmsRequest.getTemplateCode());
        return codeMap;
    }

}